﻿package kit4j.tests.jdk.awt;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

public class TestGetIcon extends JFrame implements ActionListener {
	/**
	 * {字段功能描述}
	 */
	private static final long serialVersionUID = 1L;

	private JButton getIconBtn = new JButton("get Icon");

	private JPanel iconPanel = new JPanel();

	private JTextField extField = new JTextField();

	private JLabel smallIconLabel = new JLabel("small Icon here");

	private JLabel bigIconLabel = new JLabel("big Icon here");

	public TestGetIcon() {
		this.setSize(200, 150);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());
		getIconBtn.setActionCommand("GETICON");
		getIconBtn.addActionListener(this);
		iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
		iconPanel.add(smallIconLabel);
		iconPanel.add(bigIconLabel);
		this.add(extField, BorderLayout.NORTH);
		this.add(iconPanel, BorderLayout.CENTER);
		this.add(getIconBtn, BorderLayout.SOUTH);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getActionCommand().equals("GETICON")) {
			String ext = extField.getText();
			try {
				File file = File.createTempFile("icon", "." + ext);
				FileSystemView view = FileSystemView.getFileSystemView();
				Icon smallIcon = view.getSystemIcon(file);
				Class<?> shellFolderClass = Class.forName("sun.awt.shell.ShellFolder");
				Method getShellFolderMethod = shellFolderClass.getMethod("getShellFolder", File.class);
				Method getIconMethod = shellFolderClass.getMethod("getIcon", Boolean.TYPE);
				Object shellFolder = getShellFolderMethod.invoke(null, file);
				BufferedImage bigIconImage = (BufferedImage) getIconMethod.invoke(shellFolder, true);
				Icon bigIcon = new ImageIcon(bigIconImage);
				setIconLabel(smallIcon, bigIcon);
				file.delete();
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (ClassNotFoundException e2) {
				e2.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
		}
	}

	private void setIconLabel(Icon smallIcon, Icon bigIcon) {
		smallIconLabel.setIcon(smallIcon);
		bigIconLabel.setIcon(bigIcon);
	}

	public static void main(String[] args) {
		new TestGetIcon();
	}
}